package com.fzu.mmdbproxyframework.multitask;

import java.sql.SQLException;

import com.fzu.mmdbproxyframework.context.ClusterContext;
import com.fzu.mmdbproxyframework.context.ConfigContext;
import com.fzu.mmdbproxyframework.model.DBInterf;
import com.fzu.mmdbproxyframework.model.DataNode;
import com.fzu.mmdbproxyframework.model.DataSource;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;

public class Failover {
	public MMDataNodeCluster cluster;

	public Failover(MMDataNodeCluster cluster) {
		this.cluster=cluster;
	}
	public  void creatHeartbeatTableInALLMMDB() throws SQLException{
		for(DataNode mmDataNode:cluster.getClusterContext().getMMDataNodeList()){
			for(DataSource dataSource:mmDataNode.getDb().getDataSourceList()){
				System.out.println("creatHeartbeatTableInMMDB");
				dataSource.getConnection().createStatement().execute(cluster.getConfigContext().getDropTableIfExitStr());
				dataSource.getConnection().createStatement().execute(cluster.getConfigContext().getCreateTableStr());
				dataSource.getConnection().createStatement().execute(cluster.getConfigContext().getInsertStr());
				System.out.println("creatHeartbeatTableInMMDB");
			}
		}
	}
	
	public  void creatHeartbeatTableInMMDB(DataNode mmDataNode) throws SQLException{
			for(DataSource dataSource:mmDataNode.getDb().getDataSourceList()){
				System.out.println("creatHeartbeatTableInMMDB");
				dataSource.getConnection().createStatement().execute(cluster.getConfigContext().getDropTableIfExitStr());
				dataSource.getConnection().createStatement().execute(cluster.getConfigContext().getCreateTableStr());
				dataSource.getConnection().createStatement().execute(cluster.getConfigContext().getInsertStr());
				System.out.println("creatHeartbeatTableInMMDB");
			}
	}
//	public static void creatHeartbeatTableInSourceDataNode() throws SQLException{
//		for(DBInterf db:ClusterContext.getInstance().getSourceDBNodeList()){
//			for(DataSource dataSource:db.getDataSourceList()){
//				dataSource.getConnection().createStatement().execute(ConfigContext.getInstance().getDropTableIfExitStr());
//				dataSource.getConnection().createStatement().execute(ConfigContext.getInstance().getCreateTableStr());
//				dataSource.getConnection().createStatement().execute(ConfigContext.getInstance().getInsertStr());
//			}
//		}
//		
//	}
	

}
